import axios from 'axios'

export const getLocalCity = () => {
  const localCity = JSON.parse(localStorage.getItem('local_city'))

  if (localCity) {
    return Promise.resolve(localCity)
  }

  return new Promise((resolve, reject) => {
    const myCity = new window.BMapGL.LocalCity()
    myCity.get(async result => {
      const cityName = result.name
      try{
        const { data } = await axios.get(`http://127.0.0.1:8080/area/info?name=${cityName}`)

        localStorage.setItem('local_city', JSON.stringify(data.body))

        resolve(data.body)
      } catch(e) {
        reject(e)
      }
    })
  })

}
